var app = getApp(),
  that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: '',
    imgs: [],
    roomname: '',
    imgData:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady(e) {},
  /**
   * 跳转地图定位
   */
  getCenterLocation: function(t) {
    wx.chooseLocation({
      success: function(res) {
        // success
        console.log(res, "location")
        console.log(res.latitude)
        console.log(res.longitude)
        that.setData({
          roomname: res.name,
        })

      },
      fail: function(res) {
        // fail
        wx.showToast({
          title: '获取位置失败',
        })
      },
      complete: function() {
        // complete
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    /**获取当前位置 */
    that.setData({
      city: wx.getStorageSync('ad_info').city
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**打电话 */
  ljbd: function() {
    wx.makePhoneCall({
      phoneNumber: '400-109-0351'
    })
  },
  /**上传图片 */
  // topImg:function(){
  //   console.log(1)
  //   wx.chooseImage({
  //     count: 4,
  //     sizeType: ['original', 'compressed'],
  //     sourceType: 'album',
  //     success(res) {
  //       console.log(res.tempFilePaths)
  //       // tempFilePath可以作为img标签的src属性显示图片
  //      that.setData({
  //        tempFilePaths: res.tempFilePaths
  //      })
  //     }
  //   })
  // }
  topImg: function(e) {
    var that = this;
    var imgs = this.data.imgs;
    if (imgs.length >= 4) {
      this.setData({
        lenMore: 1
      });
      setTimeout(function() {
        that.setData({
          lenMore: 0
        });
      }, 2500);
      return false;
    }
    wx.chooseImage({
      // count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        console.log(res)
        var tempFilePaths = res.tempFilePaths;
        var imgs = that.data.imgs;
        // console.log(tempFilePaths + '----');
        var successUp = 0; //成功
        var failUp = 0; //失败
        var length = tempFilePaths.length; //总数
        console.log(length)
        var count = 0; //第几张
        for (var i = 0; i < tempFilePaths.length; i++) {
          if (imgs.length >= 4) {
            that.setData({
              imgs: imgs
            });
            return false;
          } else {
            imgs.push(tempFilePaths[i]);
          }
        }
        // console.log(imgs);
        that.setData({
          imgs: imgs
        });
        that.uploadOneByOne(tempFilePaths, successUp, failUp, count, length);
      }
    });
  },
  //上传图片
  uploadOneByOne(files, successUp, failUp, count, length) {
    console.log(files, successUp, failUp, count, length)
    var that = this;
    wx.showLoading({
      title: '正在上传第' + count + '张',
    })
    const uploadTask = wx.uploadFile({
      url: 'http://172.18.4.28:8081/fileUpload/headImageUpload',
      filePath: files[count],
      name: 'files',
      success: function(e) {
        console.log(e.data)
        var t = JSON.parse(e.data)
        console.log(t)
        console.log(t.data)
        that.setData({
          imgData: t.data.concat(t.data)
        })
        successUp++;
      },
      fail: function(e) {
        console.log(e)
        var t = JSON.parse(e)
        console.log(t)
        failUp++;
      }, 
      complete: function(e) {
        count++;
        if (count == length) {
          console.log('上传成功' + successUp + ',' + '失败' + failUp);
          wx.showToast({
            title: '上传成功' + successUp,
            icon: 'success',
            duration: 2000
          })
        } else {
          that.uploadOneByOne(files, successUp, failUp, count, length);
          console.log('正在上传第' + count + '张');
        }
      }
    })

    uploadTask.onProgressUpdate((res) => {
      console.log('上传进度', res.progress)
      console.log('已经上传的数据长度', res.totalBytesSent)
      console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
    })
  },
  // 删除图片
  deleteImg: function(e) {
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgs: imgs,
    });
  },

  formSubmit: function(t) {
    console.log(this.data.imgData)
    console.log(this.data.imgs)    
    console.log(t)
    var location = t.detail.value.local,
      membername = t.detail.value.userName,
      phone = t.detail.value.mobile;
    app.getData({
      url: app.api.setRescue(),
      method: 'POST',
      data: {
        location: location,
        membername: membername,
        phone: phone,
        imgs: that.data.imgData
      },
      ok:function(res){
        console.log(res)
      }
    })
  }
})